#include <vendor/mega/hardware/common/mega_camera_enhancement/server/1.0/IWmarkServer.h>
#include <hidl/Status.h>
#include <utils/misc.h>
#include <hidl/HidlSupport.h>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include "PngTools.h"

using namespace std;
#define ICONS_COUNT 8

using ::android::sp;
using ::android::hardware::hidl_string;
using vendor::mega::hardware::common::mega_camera_enhancement::server::V1_0::BackgroundColor;
using vendor::mega::hardware::common::mega_camera_enhancement::server::V1_0::IWmarkServer;
using vendor::mega::hardware::common::mega_camera_enhancement::server::V1_0::WmarkPicture;
using vendor::mega::hardware::common::mega_camera_enhancement::server::V1_0::WmarkText;

using android::sp;
using std::string;

char *icon_paths[ICONS_COUNT] = {
    "/vendor/bin/1.png",
    "/vendor/bin/2.png",
    "/vendor/bin/3.png",
    "/vendor/bin/4.png",
    "/vendor/bin/5.png",
    "/vendor/bin/6.png",
    "/vendor/bin/7.png",
    "/vendor/bin/8.png",
};

int sendPngMsg(sp<IWmarkServer> service, uint8_t *buf, int w, int h, int ch, int x, int y, int index, int enable)
{

    WmarkPicture png;

    png.w = w;
    png.h = h;
    png.ch = ch;
    png.locate.x = x;
    png.locate.y = y;
    png.locate.index = index;
    png.locate.enable = enable;
    png.locate.faceId[0] = 2;
    png.locate.faceId[1] = 0;
    png.locate.faceId[2] = 4;
    png.locate.faceId[3] = 0;

    memcpy(&png.data[0], buf, w * h * ch);

    service->updatePicture(png);

    return 0;
}

int sendIcon(sp<IWmarkServer> service, char *icon_path, int x, int y, int index)
{
    android::pic_data out;

    android::decode_png(icon_path, &out);

    if (out.width * out.height * out.channel >= 819200)
    {
        printf("目标水印 图片大于 800kb error     info=%dx%dx%d!\n", out.width, out.height, out.channel);
        free(out.rgba);
        return -1;
    }
    sendPngMsg(service, out.rgba, out.width, out.height, out.channel, x, y, index, 1);
    free(out.rgba);

    return 0;
}

int clean(sp<IWmarkServer> service, int index, int enable)
{
    WmarkPicture png;

    png.locate.index = index;
    png.locate.enable = enable;

    service->updatePicture(png);

    return 0;
}

int cleanAll(sp<IWmarkServer> service)
{
    for (size_t i = 0; i < ICONS_COUNT; i++)
    {
        clean(service, i + 5, 0);
    }

    return 0;
    
}
int main(int argc, char **argv)
{
    sp<IWmarkServer> service = IWmarkServer::getService();
    if (service == nullptr)
    {
        printf("Failed to get service\n");
        return -1;
    }

    cleanAll(service);

    for (size_t i = 0; i < ICONS_COUNT; i++)
    {
        sendIcon(service, icon_paths[i], 100 * i, 50, i + 5);
    }
    
    printf("init.\n");
    return 0;
}